<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolBar" style="float:left;padding-top: 10px;padding-left: 50px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="shop">
          <el-select v-model="filters.shop" :clearable="true" placeholder="店铺" style="width: 10em">
            <el-option v-for="item in shops"
                       :key="item"
                       :label="item"
                       :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="sku">
          <el-input v-model="filters.sku" placeholder="SKU" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item prop="Asin">
          <el-input v-model="filters.asin" placeholder="平台Asin" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item prop="tradeMonth">
          <el-date-picker
            v-model="filters.tradeMonth"
            value-format="yyyy-MM"
            type="month"
            placeholder="统计月份"
            :picker-options="pickerOptions"
            style="width: 10em">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-search"
                     :label="$t('action.search')"
                     perms="finance:storage:view"
                     @click="findPage"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-refresh"
                     :label="$t('action.reset')"
                     perms="finance:storage:view"
                     @click="reset"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-filter"
                     label="列显示"
                     perms="finance:storage:view"
                     @click="displayFilterColumnDialog"/>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns"
                                  @handleFilterColumns="handleFilterColumns">
      </table-column-filter-dialog>
    </div>

    <!--表格内容栏-->
    <kt-table :data="pageResult" :columns="filterColumns" :execSummary="true" @findPage="findPage"></kt-table>

  </div>
</template>

<script>
  import KtTable from '../core/KtTable'
  import KtButton from '../core/KtButton'
  import TableColumnFilterDialog from '../core/TableColumnFilterDialog'

  export default {
    name: "Storage",
    components: {
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: 'small',
        filters: {
          shop: '',
          sku: '',
          asin: '',
          tradeMonth: "",
        },
        //初始表格列显示字段
        columns: [],
        //列显示过滤字段
        filterColumns: [],
        pageRequest: {},
        pageResult: {},
        shops: [],
        pickerOptions: {
          shortcuts: [{
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '上个月',
            onClick(picker) {
              const start = new Date();
              start.setMonth(start.getMonth() - 1);
              picker.$emit('pick', start);
            }
          }, {
            text: '3个月前',
            onClick(picker) {
              const start = new Date();
              start.setMonth(start.getMonth() - 3);
              picker.$emit('pick', start);
            }
          }, {
            text: '6个月前',
            onClick(picker) {
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit('pick', start);
            }
          }]
        }
      }
    },
    methods: {
      // 处理表格列过滤显示
      initColumns() {
        this.columns = [
          {prop: "shop", label: "店铺", minWidth: 100},
          {prop: "tradeMonth", label: "月份", minWidth: 100},
          {prop: "sku", label: "SKU", minWidth: 120},
          {prop: "asin", label: "ASIN", minWidth: 120},
          {prop: "quantity", label: "数量", minWidth: 100},
          {prop: "estimatedMonthlyStorageFee", label: "仓储费(￥)", sortable: true, minWidth: 120},
          {prop: "weight", label: "重量", sortable: true, minWidth: 100},
          {prop: "weightUnits", label: "重量单位", minWidth: 100},
          {prop: "itemVolume", label: "商品体积", sortable: true, minWidth: 100},
          {prop: "volumeUnits", label: "体积单位", minWidth: 100},
          {prop: "productSizeTier", label: "商品尺寸等级", minWidth: 100},
          {prop: "estimatedTotalItemVolume", label: "预估总商品体积", minWidth: 120},
          {prop: "totalIncentiveFeeAmount", label: "奖励费总额(￥)", minWidth: 120},
          {prop: "breakdownIncentiveFeeAmount", label: "细目奖励总额(￥)", minWidth: 120},
        ];
        this.filterColumns = JSON.parse(JSON.stringify(this.columns));
      },
      //处理表格列过滤显示
      displayFilterColumnDialog() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      //获取分页数据
      findPage(data) {
        if ((data !== null && typeof data !== 'undefined') && typeof data.pageRequest !== 'undefined') {
          this.pageRequest = data.pageRequest;
        }
        this.pageRequest.params = {
          sku: this.filters.sku,
          shop: this.filters.shop,
          asin: this.filters.asin,
          tradeMonth: this.filters.tradeMonth,
        };

        this.$api.storage.findPage(this.pageRequest).then((res) => {
          if (res.code === 200) {
            this.pageResult = res.data;
          }
        }).then((data !== null && typeof data !== 'undefined') ? data.callback : '');
      },
      //重置搜索条件
      reset() {
        this.$refs['filters'].resetFields();
        this.findPage();
      },
      //获取店铺信息
      findShops() {
        this.shops = localStorage.getItem('authorizedShops').split(',');
      }
    },
    mounted() {
      this.initColumns();
      this.findShops();
    }
  }
</script>

<style scoped>

</style>
